package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.GmallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class GmallController {
    @Autowired
    GmallService gmallService;
    @RequestMapping("/findTotal")
    public Object findTotal(String time){
        List<Map> list = gmallService.findTotal(time);
        System.out.println(list);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg","");
        jsonObject.put("status",0);
        jsonObject.put("data",list.get(0).get("total"));
        return jsonObject;
    }


    @RequestMapping("/findPro")
    public Object findPro(){
        List<Map> list = gmallService.findPro();
        System.out.println(list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg","");
        jsonObject.put("status",0);
        JSONObject dataObject = new JSONObject();
        JSONArray mapArray = new JSONArray();
        for (Map map : list) {
            JSONObject mapObject = new JSONObject();
            mapObject.put("name",map.get("n"));
            mapObject.put("value",map.get("total"));
            mapArray.add(mapObject);
        }
        dataObject.put("mapData",mapArray);
        dataObject.put("valueName","总成交额");
        jsonObject.put("data",dataObject);
        return jsonObject;
    }


    @RequestMapping("/findSpu")
    public Object findSpu(){
        List<Map> list = gmallService.findSpu();
        System.out.println(list);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg","");
        jsonObject.put("status",0);
        JSONObject dataObject = new JSONObject();
        JSONArray xArray = new JSONArray();
        dataObject.put("columns",xArray);

        JSONObject xObject = new JSONObject();
        xObject.put("name","spu名称");
        xObject.put("id","subsys");
        xArray.add(xObject);
        JSONObject xObject1 = new JSONObject();
        xObject1.put("name","订单金额");
        xObject1.put("id","module");
        xArray.add(xObject1);

        JSONArray yArray = new JSONArray();
        for (Map map : list) {
            JSONObject yObject = new JSONObject();
            yObject.put("subsys",map.get("n"));
            yObject.put("module",map.get("total"));
            yArray.add(yObject);
        }

        dataObject.put("rows",yArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    }

    @RequestMapping("/findTrd")
    public Object findTotal(){
        List<Map> list = gmallService.findTrd();
        System.out.println(list);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msg","");
        jsonObject.put("status",0);
        JSONObject dataObject = new JSONObject();
        JSONArray xArray = new JSONArray();
        for (Map map : list) {
            xArray.add(map.get("n")) ;
        }
        dataObject.put("categories",xArray);
        JSONArray yArray = new JSONArray();
        JSONObject yObject = new JSONObject();
        yObject.put("name","交易额");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("total"));
        }
        yObject.put("data",dataArray);
        yArray.add(yObject);
        dataObject.put("series",yArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    }
}
